
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>Go 语言条件语句 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="../part10/" />
    
    
    <link rel="prev" href="../part8/" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../part1/">
            
                <a href="../part1/">
            
                    
                    Go 语言简介
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../part2/">
            
                <a href="../part2/">
            
                    
                    Go 语言环境安装
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../part3/">
            
                <a href="../part3/">
            
                    
                    Go 语言结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../part4/">
            
                <a href="../part4/">
            
                    
                    Go 语言基础语法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../part5/">
            
                <a href="../part5/">
            
                    
                    Go 语言数据类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../part6/">
            
                <a href="../part6/">
            
                    
                    Go 语言变量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../part7/">
            
                <a href="../part7/">
            
                    
                    Go 语言常量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../part8/">
            
                <a href="../part8/">
            
                    
                    Go 语言运算符
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.10" data-path="./">
            
                <a href="./">
            
                    
                    Go 语言条件语句
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="../part10/">
            
                <a href="../part10/">
            
                    
                    Go 语言循环语句
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="../part11/">
            
                <a href="../part11/">
            
                    
                    Go 语言函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="../part12/">
            
                <a href="../part12/">
            
                    
                    Go 语言数组
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="../part13/">
            
                <a href="../part13/">
            
                    
                    Go 语言指针
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="../part14/">
            
                <a href="../part14/">
            
                    
                    Go 语言结构体
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16" data-path="../part15/">
            
                <a href="../part15/">
            
                    
                    Go 语言切片(Slice)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17" data-path="../part16/">
            
                <a href="../part16/">
            
                    
                    Go 语言范围(Range)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18" data-path="../part17/">
            
                <a href="../part17/">
            
                    
                    Go 语言Map(集合)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.19" data-path="../part18/">
            
                <a href="../part18/">
            
                    
                    Go 语言递归函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.20" data-path="../part19/">
            
                <a href="../part19/">
            
                    
                    Go 语言类型转换
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.21" data-path="../part20/">
            
                <a href="../part20/">
            
                    
                    Go 语言接口
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.22" data-path="../part21/">
            
                <a href="../part21/">
            
                    
                    Go 错误处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.23" data-path="../part22/">
            
                <a href="../part22/">
            
                    
                    Go 并发
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >Go 语言条件语句</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="go-&#x8BED;&#x8A00;&#x6761;&#x4EF6;&#x8BED;&#x53E5;">Go &#x8BED;&#x8A00;&#x6761;&#x4EF6;&#x8BED;&#x53E5;</h1>
<p>&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#x6761;&#x4EF6;&#x8BED;&#x53E5;&#x9700;&#x8981;&#x5F00;&#x53D1;&#x8005;&#x901A;&#x8FC7;&#x6307;&#x5B9A;&#x4E00;&#x4E2A;&#x6216;&#x591A;&#x4E2A;&#x6761;&#x4EF6;&#xFF0C;&#x5E76;&#x901A;&#x8FC7;&#x6D4B;&#x8BD5;&#x6761;&#x4EF6;&#x662F;&#x5426;&#x4E3A; true &#x6765;&#x51B3;&#x5B9A;&#x662F;&#x5426;&#x6267;&#x884C;&#x6307;&#x5B9A;&#x8BED;&#x53E5;&#xFF0C;&#x5E76;&#x5728;&#x6761;&#x4EF6;&#x4E3A; false &#x7684;&#x60C5;&#x51B5;&#x5728;&#x6267;&#x884C;&#x53E6;&#x5916;&#x7684;&#x8BED;&#x53E5;&#x3002;</p>
<p>&#x4E0B;&#x56FE;&#x5C55;&#x793A;&#x4E86;&#x7A0B;&#x5E8F;&#x8BED;&#x8A00;&#x4E2D;&#x6761;&#x4EF6;&#x8BED;&#x53E5;&#x7684;&#x7ED3;&#x6784;&#xFF1A;</p>
<center>
    <img style="border-radius: 0.3125em;
    box-shadow: 0 2px 4px 0 rgba(34,36,38,.12),0 2px 10px 0 rgba(34,36,38,.08);" src="../source/images/part9/01.png">
    <br>
    <div style="color:orange; border-bottom: 0px solid #d9d9d9;
    display: inline-block;
    color: #999;ds
    padding: 5px;"></div>
</center>

<p>Go &#x8BED;&#x8A00;&#x63D0;&#x4F9B;&#x4E86;&#x4EE5;&#x4E0B;&#x51E0;&#x79CD;&#x6761;&#x4EF6;&#x5224;&#x65AD;&#x8BED;&#x53E5;&#xFF1A;</p>
<table>
    <tr>
        <th bgcolor="#4b4b4b"><font color="#ffffff">&#x8BED;&#x53E5;</font></th>
        <th bgcolor="#4b4b4b"><font color="#ffffff">&#x63CF;&#x8FF0;</font></th>
    </tr>
    <tr>        
        <td align="center" bgcolor="#ffffff">if&#x8BED;&#x53E5;</td>
        <td align="center" bgcolor="#ffffff">if &#x8BED;&#x53E5; &#x7531;&#x4E00;&#x4E2A;&#x5E03;&#x5C14;&#x8868;&#x8FBE;&#x5F0F;&#x540E;&#x7D27;&#x8DDF;&#x4E00;&#x4E2A;&#x6216;&#x591A;&#x4E2A;&#x8BED;&#x53E5;&#x7EC4;&#x6210;&#x3002;</td>
    </tr>
    <tr>        
        <td align="center" bgcolor="#f5f2ee">if...else &#x8BED;&#x53E5;</td>
        <td align="center" bgcolor="#f5f2ee">if &#x8BED;&#x53E5; &#x540E;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x53EF;&#x9009;&#x7684; else &#x8BED;&#x53E5;, else &#x8BED;&#x53E5;&#x4E2D;&#x7684;&#x8868;&#x8FBE;&#x5F0F;&#x5728;&#x5E03;&#x5C14;&#x8868;&#x8FBE;&#x5F0F;&#x4E3A; false &#x65F6;&#x6267;&#x884C;&#x3002;</td>
    </tr>
    <tr>        
        <td align="center" bgcolor="#ffffff">if &#x5D4C;&#x5957;&#x8BED;&#x53E5;</td>
        <td align="center" bgcolor="#ffffff">&#x4F60;&#x53EF;&#x4EE5;&#x5728; if &#x6216; else if &#x8BED;&#x53E5;&#x4E2D;&#x5D4C;&#x5165;&#x4E00;&#x4E2A;&#x6216;&#x591A;&#x4E2A; if &#x6216; else if &#x8BED;&#x53E5;&#x3002;</td>
    </tr>
    <tr>        
        <td align="center" bgcolor="#f5f2ee">switch &#x8BED;&#x53E5;</td>
        <td align="center" bgcolor="#f5f2ee">switch &#x8BED;&#x53E5;&#x7528;&#x4E8E;&#x57FA;&#x4E8E;&#x4E0D;&#x540C;&#x6761;&#x4EF6;&#x6267;&#x884C;&#x4E0D;&#x540C;&#x52A8;&#x4F5C;&#x3002;</td>
    </tr>
    <tr>        
        <td align="center" bgcolor="#ffffff">select &#x8BED;&#x53E5;</td>
        <td align="center" bgcolor="#ffffff">select &#x8BED;&#x53E5;&#x7C7B;&#x4F3C;&#x4E8E; switch &#x8BED;&#x53E5;&#xFF0C;&#x4F46;&#x662F;select&#x4F1A;&#x968F;&#x673A;&#x6267;&#x884C;&#x4E00;&#x4E2A;&#x53EF;&#x8FD0;&#x884C;&#x7684;case&#x3002;&#x5982;&#x679C;&#x6CA1;&#x6709;case&#x53EF;&#x8FD0;&#x884C;&#xFF0C;&#x5B83;&#x5C06;&#x963B;&#x585E;&#xFF0C;&#x76F4;&#x5230;&#x6709;case&#x53EF;&#x8FD0;&#x884C;&#x3002;</td>
    </tr>
</table>

<blockquote>
<p>&#x6CE8;&#x610F;&#xFF1A;Go &#x6CA1;&#x6709;&#x4E09;&#x76EE;&#x8FD0;&#x7B97;&#x7B26;&#xFF0C;&#x6240;&#x4EE5;&#x4E0D;&#x652F;&#x6301; ?: &#x5F62;&#x5F0F;&#x7684;&#x6761;&#x4EF6;&#x5224;&#x65AD;&#x3002;</p>
</blockquote>
<hr>
<h2 id="if-&#x8BED;&#x53E5;">if &#x8BED;&#x53E5;</h2>
<p>if &#x8BED;&#x53E5;&#x7531;&#x5E03;&#x5C14;&#x8868;&#x8FBE;&#x5F0F;&#x540E;&#x7D27;&#x8DDF;&#x4E00;&#x4E2A;&#x6216;&#x591A;&#x4E2A;&#x8BED;&#x53E5;&#x7EC4;&#x6210;&#x3002;</p>
<h3 id="&#x8BED;&#x6CD5;">&#x8BED;&#x6CD5;</h3>
<p>Go &#x7F16;&#x7A0B;&#x8BED;&#x8A00;&#x4E2D; if &#x8BED;&#x53E5;&#x7684;&#x8BED;&#x6CD5;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">if</span> &#x5E03;&#x5C14;&#x8868;&#x8FBE;&#x5F0F; {
   <span class="hljs-comment">/* &#x5728;&#x5E03;&#x5C14;&#x8868;&#x8FBE;&#x5F0F;&#x4E3A; true &#x65F6;&#x6267;&#x884C; */</span>
}
</code></pre>
<p>If &#x5728;&#x5E03;&#x5C14;&#x8868;&#x8FBE;&#x5F0F;&#x4E3A; true &#x65F6;&#xFF0C;&#x5176;&#x540E;&#x7D27;&#x8DDF;&#x7684;&#x8BED;&#x53E5;&#x5757;&#x6267;&#x884C;&#xFF0C;&#x5982;&#x679C;&#x4E3A; false &#x5219;&#x4E0D;&#x6267;&#x884C;&#x3002;
&#x6D41;&#x7A0B;&#x56FE;&#x5982;&#x4E0B;&#xFF1A;</p>
<center>
    <img style="border-radius: 0.3125em;
    box-shadow: 0 2px 4px 0 rgba(34,36,38,.12),0 2px 10px 0 rgba(34,36,38,.08);" src="../source/images/part9/01.png">
    <br>
    <div style="color:orange; border-bottom: 0px solid #d9d9d9;
    display: inline-block;
    color: #999;ds
    padding: 5px;">if &#x8BED;&#x53E5;</div>
</center>


<p>&#x5B9E;&#x4F8B;&#xFF1A;&#x4F7F;&#x7528; if &#x5224;&#x65AD;&#x4E00;&#x4E2A;&#x6570;&#x53D8;&#x91CF;&#x7684;&#x5927;&#x5C0F;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">package</span> main

<span class="hljs-keyword">import</span> <span class="hljs-string">&quot;fmt&quot;</span>

<span class="hljs-keyword">func</span> main() {
    <span class="hljs-comment">/* &#x5B9A;&#x4E49;&#x5C40;&#x90E8;&#x53D8;&#x91CF; */</span>
    <span class="hljs-keyword">var</span> a <span class="hljs-keyword">int</span> = <span class="hljs-number">10</span>

    <span class="hljs-comment">/* &#x4F7F;&#x7528; if &#x8BED;&#x53E5;&#x5224;&#x65AD;&#x5E03;&#x5C14;&#x8868;&#x8FBE;&#x5F0F; */</span>
    <span class="hljs-keyword">if</span> a &lt; <span class="hljs-number">20</span> {
        <span class="hljs-comment">/* &#x5982;&#x679C;&#x6761;&#x4EF6;&#x4E3A; true &#x5219;&#x6267;&#x884C;&#x4EE5;&#x4E0B;&#x8BED;&#x53E5; */</span>
        fmt.Printf(<span class="hljs-string">&quot;a &#x5C0F;&#x4E8E; 20\n&quot;</span> )
    }
    fmt.Printf(<span class="hljs-string">&quot;a &#x7684;&#x503C;&#x4E3A; : %d\n&quot;</span>, a)
    }
</code></pre>
<p>&#x4EE5;&#x4E0A;&#x4EE3;&#x7801;&#x6267;&#x884C;&#x7ED3;&#x679C;&#x4E3A;&#xFF1A;</p>
<pre><code class="lang-sh">a &#x5C0F;&#x4E8E; 20
a &#x7684;&#x503C;&#x4E3A; : 10
</code></pre>
<h3 id="some-test">some test</h3>
<ol>
<li><p>&#x7528; If &#x8BED;&#x53E5;&#x5224;&#x65AD;&#x5076;&#x6570;</p>
<pre><code class="lang-go"> <span class="hljs-keyword">package</span> main

 <span class="hljs-keyword">import</span> <span class="hljs-string">&quot;fmt&quot;</span>

 <span class="hljs-keyword">func</span> main() {
     <span class="hljs-keyword">var</span> s <span class="hljs-keyword">int</span> ;    <span class="hljs-comment">// &#x58F0;&#x660E;&#x53D8;&#x91CF; s &#x662F;&#x9700;&#x8981;&#x5224;&#x65AD;&#x7684;&#x6570;</span>
     fmt.Println(<span class="hljs-string">&quot;&#x8F93;&#x5165;&#x4E00;&#x4E2A;&#x6570;&#x5B57;&#xFF1A;&quot;</span>)
     fmt.Scan(&amp;s)

     <span class="hljs-keyword">if</span> s%<span class="hljs-number">2</span> == <span class="hljs-number">0</span>  { <span class="hljs-comment">//     &#x53D6; s &#x5904;&#x4EE5; 2 &#x7684;&#x4F59;&#x6570;&#x662F;&#x5426;&#x7B49;&#x4E8E; 0</span>
         fmt.Print(<span class="hljs-string">&quot;s &#x662F;&#x5076;&#x6570;\n&quot;</span>) ;<span class="hljs-comment">//&#x5982;&#x679C;&#x6210;&#x7ACB;</span>
     }<span class="hljs-keyword">else</span> {
         fmt.Print(<span class="hljs-string">&quot;s &#x4E0D;&#x662F;&#x5076;&#x6570;\n&quot;</span>) ;<span class="hljs-comment">//&#x5426;&#x5219;</span>
     }
     fmt.Print(<span class="hljs-string">&quot;s &#x7684;&#x503C;&#x662F;&#xFF1A;&quot;</span>,s) ;
 }
</code></pre>
</li>
<li><p>Go &#x7684; if &#x8FD8;&#x6709;&#x4E00;&#x4E2A;&#x5F3A;&#x5927;&#x7684;&#x5730;&#x65B9;&#x5C31;&#x662F;&#x6761;&#x4EF6;&#x5224;&#x65AD;&#x8BED;&#x53E5;&#x91CC;&#x9762;&#x5141;&#x8BB8;&#x58F0;&#x660E;&#x4E00;&#x4E2A;&#x53D8;&#x91CF;&#xFF0C;&#x8FD9;&#x4E2A;&#x53D8;&#x91CF;&#x7684;&#x4F5C;&#x7528;&#x57DF;&#x53EA;&#x80FD;&#x5728;&#x8BE5;&#x6761;&#x4EF6;&#x903B;&#x8F91;&#x5757;&#x5185;&#xFF0C;&#x5176;&#x4ED6;&#x5730;&#x65B9;&#x5C31;&#x4E0D;&#x8D77;&#x4F5C;&#x7528;&#x4E86;&#xFF0C;&#x5982;&#x4E0B;&#x6240;&#x793A;:</p>
<pre><code class="lang-go"> <span class="hljs-keyword">package</span> main

 <span class="hljs-keyword">import</span> <span class="hljs-string">&quot;fmt&quot;</span>
 <span class="hljs-keyword">func</span> main() {
     <span class="hljs-keyword">if</span> num := <span class="hljs-number">9</span>; num &lt; <span class="hljs-number">0</span> {
         fmt.Println(num, <span class="hljs-string">&quot;is negative&quot;</span>)
     } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> num &lt; <span class="hljs-number">10</span> {
         fmt.Println(num, <span class="hljs-string">&quot;has 1 digit&quot;</span>)
     } <span class="hljs-keyword">else</span> {
         fmt.Println(num, <span class="hljs-string">&quot;has multiple digits&quot;</span>)
     }
 }
</code></pre>
<p> &#x8FD0;&#x884C;&#x7ED3;&#x679C;&#xFF1A;</p>
<pre><code class="lang-sh"> 9 has 1 digit
</code></pre>
<p> &#x683C;&#x5F0F;&#xFF1A;</p>
<pre><code class="lang-go"> <span class="hljs-keyword">if</span> statement; condition {  
 }
</code></pre>
<pre><code class="lang-go"> <span class="hljs-keyword">package</span> main

 <span class="hljs-keyword">import</span> (  
     <span class="hljs-string">&quot;fmt&quot;</span>
 )

 <span class="hljs-keyword">func</span> main() {  
     <span class="hljs-keyword">if</span> num := <span class="hljs-number">10</span>; num % <span class="hljs-number">2</span> == <span class="hljs-number">0</span> { <span class="hljs-comment">// &#x5224;&#x65AD;&#x6570;&#x5B57;&#x662F;&#x5426;&#x4E3A;&#x5076;&#x6570;</span>
         fmt.Println(num,<span class="hljs-string">&quot;&#x5076;&#x6570;&quot;</span>) 
     }  <span class="hljs-keyword">else</span> {
         fmt.Println(num,<span class="hljs-string">&quot;&#x5947;&#x6570;&quot;</span>)
     }
 }
</code></pre>
</li>
</ol>
<ol>
<li>if &#x8BED;&#x53E5;&#x4F7F;&#x7528; tips&#xFF1A;<ul>
<li>&#x4E0D;&#x9700;&#x4F7F;&#x7528;&#x62EC;&#x53F7;&#x5C06;&#x6761;&#x4EF6;&#x5305;&#x542B;&#x8D77;&#x6765;</li>
<li>&#x5927;&#x62EC;&#x53F7;{}&#x5FC5;&#x987B;&#x5B58;&#x5728;&#xFF0C;&#x5373;&#x4F7F;&#x53EA;&#x6709;&#x4E00;&#x884C;&#x8BED;&#x53E5;</li>
<li>&#x5DE6;&#x62EC;&#x53F7;&#x5FC5;&#x987B;&#x5728;if&#x6216;else&#x7684;&#x540C;&#x4E00;&#x884C;</li>
<li>&#x5728;if&#x4E4B;&#x540E;&#xFF0C;&#x6761;&#x4EF6;&#x8BED;&#x53E5;&#x4E4B;&#x524D;&#xFF0C;&#x53EF;&#x4EE5;&#x6DFB;&#x52A0;&#x53D8;&#x91CF;&#x521D;&#x59CB;&#x5316;&#x8BED;&#x53E5;&#xFF0C;&#x4F7F;&#x7528;&#xFF1B;&#x8FDB;&#x884C;&#x5206;&#x9694;</li>
<li>&#x5728;&#x6709;&#x8FD4;&#x56DE;&#x503C;&#x7684;&#x51FD;&#x6570;&#x4E2D;&#xFF0C;&#x6700;&#x7EC8;&#x7684;return&#x4E0D;&#x80FD;&#x5728;&#x6761;&#x4EF6;&#x8BED;&#x53E5;&#x4E2D;</li>
</ul>
</li>
</ol>
<hr>
<h2 id="ifelse-&#x8BED;&#x53E5;">if...else &#x8BED;&#x53E5;</h2>
<p>if &#x8BED;&#x53E5; &#x540E;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x53EF;&#x9009;&#x7684; else &#x8BED;&#x53E5;, else &#x8BED;&#x53E5;&#x4E2D;&#x7684;&#x8868;&#x8FBE;&#x5F0F;&#x5728;&#x5E03;&#x5C14;&#x8868;&#x8FBE;&#x5F0F;&#x4E3A; false &#x65F6;&#x6267;&#x884C;&#x3002;</p>
<h3 id="&#x8BED;&#x6CD5;">&#x8BED;&#x6CD5;</h3>
<p>Go &#x7F16;&#x7A0B;&#x8BED;&#x8A00;&#x4E2D; if...else &#x8BED;&#x53E5;&#x7684;&#x8BED;&#x6CD5;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">if</span> &#x5E03;&#x5C14;&#x8868;&#x8FBE;&#x5F0F; {
   <span class="hljs-comment">/* &#x5728;&#x5E03;&#x5C14;&#x8868;&#x8FBE;&#x5F0F;&#x4E3A; true &#x65F6;&#x6267;&#x884C; */</span>
} <span class="hljs-keyword">else</span> {
  <span class="hljs-comment">/* &#x5728;&#x5E03;&#x5C14;&#x8868;&#x8FBE;&#x5F0F;&#x4E3A; false &#x65F6;&#x6267;&#x884C; */</span>
}
</code></pre>
<p>If &#x5728;&#x5E03;&#x5C14;&#x8868;&#x8FBE;&#x5F0F;&#x4E3A; true &#x65F6;&#xFF0C;&#x5176;&#x540E;&#x7D27;&#x8DDF;&#x7684;&#x8BED;&#x53E5;&#x5757;&#x6267;&#x884C;&#xFF0C;&#x5982;&#x679C;&#x4E3A; false &#x5219;&#x6267;&#x884C; else &#x8BED;&#x53E5;&#x5757;&#x3002;
&#x6D41;&#x7A0B;&#x56FE;&#x5982;&#x4E0B;&#xFF1A;</p>
<center>
    <img style="border-radius: 0.3125em;
    box-shadow: 0 2px 4px 0 rgba(34,36,38,.12),0 2px 10px 0 rgba(34,36,38,.08);" src="../source/images/part9/02.png">
    <br>
    <div style="color:orange; border-bottom: 0px solid #d9d9d9;
    display: inline-block;
    color: #999;ds
    padding: 5px;">if...else &#x8BED;&#x53E5;</div>
</center>

<p>&#x5B9E;&#x4F8B;&#xFF1A;&#x4F7F;&#x7528; if else &#x5224;&#x65AD;&#x4E00;&#x4E2A;&#x6570;&#x7684;&#x5927;&#x5C0F;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">package</span> main

<span class="hljs-keyword">import</span> <span class="hljs-string">&quot;fmt&quot;</span>

<span class="hljs-keyword">func</span> main() {
    <span class="hljs-comment">/* &#x5C40;&#x90E8;&#x53D8;&#x91CF;&#x5B9A;&#x4E49; */</span>
    <span class="hljs-keyword">var</span> a <span class="hljs-keyword">int</span> = <span class="hljs-number">100</span>;

    <span class="hljs-comment">/* &#x5224;&#x65AD;&#x5E03;&#x5C14;&#x8868;&#x8FBE;&#x5F0F; */</span>
    <span class="hljs-keyword">if</span> a &lt; <span class="hljs-number">20</span> {
        <span class="hljs-comment">/* &#x5982;&#x679C;&#x6761;&#x4EF6;&#x4E3A; true &#x5219;&#x6267;&#x884C;&#x4EE5;&#x4E0B;&#x8BED;&#x53E5; */</span>
        fmt.Printf(<span class="hljs-string">&quot;a &#x5C0F;&#x4E8E; 20\n&quot;</span> );
    } <span class="hljs-keyword">else</span> {
        <span class="hljs-comment">/* &#x5982;&#x679C;&#x6761;&#x4EF6;&#x4E3A; false &#x5219;&#x6267;&#x884C;&#x4EE5;&#x4E0B;&#x8BED;&#x53E5; */</span>
        fmt.Printf(<span class="hljs-string">&quot;a &#x4E0D;&#x5C0F;&#x4E8E; 20\n&quot;</span> );
    }
    fmt.Printf(<span class="hljs-string">&quot;a &#x7684;&#x503C;&#x4E3A; : %d\n&quot;</span>, a);

}
</code></pre>
<p>&#x4EE5;&#x4E0A;&#x4EE3;&#x7801;&#x6267;&#x884C;&#x7ED3;&#x679C;&#x4E3A;&#xFF1A;</p>
<pre><code class="lang-sh">a &#x4E0D;&#x5C0F;&#x4E8E; 20
a &#x7684;&#x503C;&#x4E3A; : 100
</code></pre>
<h3 id="some-test">some test</h3>
<ul>
<li><p>&#x5BFB;&#x627E;&#x5230; 100 &#x4EE5;&#x5185;&#x7684;&#x6240;&#x6709;&#x7684;&#x7D20;&#x6570;:</p>
<pre><code class="lang-go">  <span class="hljs-keyword">package</span> main

  <span class="hljs-keyword">import</span> <span class="hljs-string">&quot;fmt&quot;</span>

  <span class="hljs-keyword">func</span> main(){
      <span class="hljs-comment">// var count,c int   //&#x5B9A;&#x4E49;&#x53D8;&#x91CF;&#x4E0D;&#x4F7F;&#x7528;&#x4E5F;&#x4F1A;&#x62A5;&#x9519;</span>
      <span class="hljs-keyword">var</span> count <span class="hljs-keyword">int</span>
      <span class="hljs-keyword">var</span> flag <span class="hljs-keyword">bool</span>
      count=<span class="hljs-number">1</span>
      <span class="hljs-comment">//while(count&lt;100) {    //go&#x6CA1;&#x6709;while</span>
      <span class="hljs-keyword">for</span> count &lt; <span class="hljs-number">100</span> {
          count++
          flag = <span class="hljs-literal">true</span>
          <span class="hljs-comment">//&#x6CE8;&#x610F;tmp&#x53D8;&#x91CF;  :=</span>
          <span class="hljs-keyword">for</span> tmp:=<span class="hljs-number">2</span>;tmp&lt;count;tmp++ {
              <span class="hljs-keyword">if</span> count%tmp==<span class="hljs-number">0</span>{
                  flag = <span class="hljs-literal">false</span>
              }
          }

          <span class="hljs-comment">// &#x6BCF;&#x4E00;&#x4E2A; if else &#x90FD;&#x9700;&#x8981;&#x52A0;&#x5165;&#x62EC;&#x53F7; &#x540C;&#x65F6; else &#x4F4D;&#x7F6E;&#x4E0D;&#x80FD;&#x5728;&#x65B0;&#x4E00;&#x884C;</span>
          <span class="hljs-keyword">if</span> flag == <span class="hljs-literal">true</span> {
              fmt.Println(count, <span class="hljs-string">&quot;&#x7D20;&#x6570;&quot;</span>)
          }<span class="hljs-keyword">else</span>{
              <span class="hljs-keyword">continue</span>
          }
      }
  }
</code></pre>
</li>
</ul>
<hr>
<h2 id="if-&#x8BED;&#x53E5;&#x5D4C;&#x5957;">if &#x8BED;&#x53E5;&#x5D4C;&#x5957;</h2>
<p>&#x4F60;&#x53EF;&#x4EE5;&#x5728; if &#x6216; else if &#x8BED;&#x53E5;&#x4E2D;&#x5D4C;&#x5165;&#x4E00;&#x4E2A;&#x6216;&#x591A;&#x4E2A; if &#x6216; else if &#x8BED;&#x53E5;&#x3002;</p>
<h3 id="&#x8BED;&#x6CD5;">&#x8BED;&#x6CD5;</h3>
<p>Go &#x7F16;&#x7A0B;&#x8BED;&#x8A00;&#x4E2D; if...else &#x8BED;&#x53E5;&#x7684;&#x8BED;&#x6CD5;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">if</span> &#x5E03;&#x5C14;&#x8868;&#x8FBE;&#x5F0F; <span class="hljs-number">1</span> {
    <span class="hljs-comment">/* &#x5728;&#x5E03;&#x5C14;&#x8868;&#x8FBE;&#x5F0F; 1 &#x4E3A; true &#x65F6;&#x6267;&#x884C; */</span>
    <span class="hljs-keyword">if</span> &#x5E03;&#x5C14;&#x8868;&#x8FBE;&#x5F0F; <span class="hljs-number">2</span> {
        <span class="hljs-comment">/* &#x5728;&#x5E03;&#x5C14;&#x8868;&#x8FBE;&#x5F0F; 2 &#x4E3A; true &#x65F6;&#x6267;&#x884C; */</span>
    }
}
</code></pre>
<p>&#x4F60;&#x53EF;&#x4EE5;&#x4EE5;&#x540C;&#x6837;&#x7684;&#x65B9;&#x5F0F;&#x5728; if &#x8BED;&#x53E5;&#x4E2D;&#x5D4C;&#x5957; else if...else &#x8BED;&#x53E5;</p>
<p>&#x5B9E;&#x4F8B;&#xFF1A;&#x5D4C;&#x5957;&#x4F7F;&#x7528; if &#x8BED;&#x53E5;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">package</span> main

<span class="hljs-keyword">import</span> <span class="hljs-string">&quot;fmt&quot;</span>

<span class="hljs-keyword">func</span> main() {
    <span class="hljs-comment">/* &#x5B9A;&#x4E49;&#x5C40;&#x90E8;&#x53D8;&#x91CF; */</span>
    <span class="hljs-keyword">var</span> a <span class="hljs-keyword">int</span> = <span class="hljs-number">100</span>
    <span class="hljs-keyword">var</span> b <span class="hljs-keyword">int</span> = <span class="hljs-number">200</span>

    <span class="hljs-comment">/* &#x5224;&#x65AD;&#x6761;&#x4EF6; */</span>
    <span class="hljs-keyword">if</span> a == <span class="hljs-number">100</span> {
        <span class="hljs-comment">/* if &#x6761;&#x4EF6;&#x8BED;&#x53E5;&#x4E3A; true &#x6267;&#x884C; */</span>
        <span class="hljs-keyword">if</span> b == <span class="hljs-number">200</span> {
            <span class="hljs-comment">/* if &#x6761;&#x4EF6;&#x8BED;&#x53E5;&#x4E3A; true &#x6267;&#x884C; */</span>
            fmt.Printf(<span class="hljs-string">&quot;a &#x7684;&#x503C;&#x4E3A; 100 &#xFF0C; b &#x7684;&#x503C;&#x4E3A; 200\n&quot;</span> )
        }
    }
    fmt.Printf(<span class="hljs-string">&quot;a &#x503C;&#x4E3A; : %d\n&quot;</span>, a )
    fmt.Printf(<span class="hljs-string">&quot;b &#x503C;&#x4E3A; : %d\n&quot;</span>, b )
}
</code></pre>
<p>&#x4EE5;&#x4E0A;&#x4EE3;&#x7801;&#x6267;&#x884C;&#x7ED3;&#x679C;&#x4E3A;&#xFF1A;</p>
<pre><code class="lang-sh">a &#x7684;&#x503C;&#x4E3A; 100 &#xFF0C; b &#x7684;&#x503C;&#x4E3A; 200
a &#x503C;&#x4E3A; : 100
b &#x503C;&#x4E3A; : 200
</code></pre>
<h3 id="some-test">some test</h3>
<ul>
<li>&#x5224;&#x65AD;&#x7528;&#x6237;&#x5BC6;&#x7801;&#x8F93;&#x5165;&#xFF1A;</li>
</ul>
<pre><code class="lang-go"><span class="hljs-keyword">package</span> main 

<span class="hljs-keyword">import</span><span class="hljs-string">&quot;fmt&quot;</span>

<span class="hljs-keyword">func</span> main(){
    <span class="hljs-keyword">var</span> a <span class="hljs-keyword">int</span> 
    <span class="hljs-keyword">var</span> b <span class="hljs-keyword">int</span>
    fmt.Printf(<span class="hljs-string">&quot;&#x8BF7;&#x8F93;&#x5165;&#x5BC6;&#x7801;&#xFF1A;   \n&quot;</span>)
    fmt.Scan(&amp;a)
    <span class="hljs-keyword">if</span> a == <span class="hljs-number">5211314</span> {
    fmt.Printf(<span class="hljs-string">&quot;&#x8BF7;&#x518D;&#x6B21;&#x8F93;&#x5165;&#x5BC6;&#x7801;&#xFF1A;&quot;</span>)
    fmt.Scan(&amp;b)
        <span class="hljs-keyword">if</span> b == <span class="hljs-number">5211314</span> {
            fmt.Printf(<span class="hljs-string">&quot;&#x5BC6;&#x7801;&#x6B63;&#x786E;&#xFF0C;&#x95E8;&#x9501;&#x5DF2;&#x6253;&#x5F00;&quot;</span>)
        }<span class="hljs-keyword">else</span>{
            fmt.Printf(<span class="hljs-string">&quot;&#x975E;&#x6CD5;&#x5165;&#x4FB5;&#xFF0C;&#x5DF2;&#x81EA;&#x52A8;&#x62A5;&#x8B66;&quot;</span>)
        }
    }<span class="hljs-keyword">else</span>{
        fmt.Printf(<span class="hljs-string">&quot;&#x975E;&#x6CD5;&#x5165;&#x4FB5;&#xFF0C;&#x5DF2;&#x81EA;&#x52A8;&#x62A5;&#x8B66;&quot;</span>)
    }
}
</code></pre>
<hr>
<h2 id="switch-&#x8BED;&#x53E5;">switch &#x8BED;&#x53E5;</h2>
<p>&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;switch &#x8BED;&#x53E5;&#x7528;&#x4E8E;&#x57FA;&#x4E8E;&#x4E0D;&#x540C;&#x6761;&#x4EF6;&#x6267;&#x884C;&#x4E0D;&#x540C;&#x52A8;&#x4F5C;&#xFF0C;&#x6BCF;&#x4E00;&#x4E2A; case &#x5206;&#x652F;&#x90FD;&#x662F;&#x552F;&#x4E00;&#x7684;&#xFF0C;&#x4ECE;&#x4E0A;&#x81F3;&#x4E0B;&#x9010;&#x4E00;&#x6D4B;&#x8BD5;&#xFF0C;&#x76F4;&#x5230;&#x5339;&#x914D;&#x4E3A;&#x6B62;&#x3002;</p>
<p>&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;switch &#x8BED;&#x53E5;&#x6267;&#x884C;&#x7684;&#x8FC7;&#x7A0B;&#x4ECE;&#x4E0A;&#x81F3;&#x4E0B;&#xFF0C;&#x76F4;&#x5230;&#x627E;&#x5230;&#x5339;&#x914D;&#x9879;&#xFF0C;&#x5339;&#x914D;&#x9879;&#x540E;&#x9762;&#x4E5F;&#x4E0D;&#x9700;&#x8981;&#x518D;&#x52A0; break&#x3002;</p>
<p>&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;switch &#x9ED8;&#x8BA4;&#x60C5;&#x51B5;&#x4E0B; case &#x6700;&#x540E;&#x81EA;&#x5E26; break &#x8BED;&#x53E5;&#xFF0C;&#x5339;&#x914D;&#x6210;&#x529F;&#x540E;&#x5C31;&#x4E0D;&#x4F1A;&#x6267;&#x884C;&#x5176;&#x4ED6; case&#xFF0C;&#x5982;&#x679C;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x6267;&#x884C;&#x540E;&#x9762;&#x7684; case&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; fallthrough &#x3002;</p>
<h3 id="&#x8BED;&#x6CD5;">&#x8BED;&#x6CD5;</h3>
<p>Go &#x7F16;&#x7A0B;&#x8BED;&#x8A00;&#x4E2D; switch &#x8BED;&#x53E5;&#x7684;&#x8BED;&#x6CD5;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">switch</span> var1 {
    <span class="hljs-keyword">case</span> val1:
        ...
    <span class="hljs-keyword">case</span> val2:
        ...
    <span class="hljs-keyword">default</span>:
        ...
}
</code></pre>
<p>&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#x53D8;&#x91CF; var1 &#x53EF;&#x4EE5;&#x662F;&#x4EFB;&#x4F55;&#x7C7B;&#x578B;&#xFF0C;&#x800C; val1 &#x548C; val2 &#x5219;&#x53EF;&#x4EE5;&#x662F;&#x540C;&#x7C7B;&#x578B;&#x7684;&#x4EFB;&#x610F;&#x503C;&#x3002;&#x7C7B;&#x578B;&#x4E0D;&#x88AB;&#x5C40;&#x9650;&#x4E8E;&#x5E38;&#x91CF;&#x6216;&#x6574;&#x6570;&#xFF0C;&#x4F46;&#x5FC5;&#x987B;&#x662F;&#x76F8;&#x540C;&#x7684;&#x7C7B;&#x578B;&#xFF1B;&#x6216;&#x8005;&#x6700;&#x7EC8;&#x7ED3;&#x679C;&#x4E3A;&#x76F8;&#x540C;&#x7C7B;&#x578B;&#x7684;&#x8868;&#x8FBE;&#x5F0F;&#x3002;</p>
<p>&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#x60A8;&#x53EF;&#x4EE5;&#x540C;&#x65F6;&#x6D4B;&#x8BD5;&#x591A;&#x4E2A;&#x53EF;&#x80FD;&#x7B26;&#x5408;&#x6761;&#x4EF6;&#x7684;&#x503C;&#xFF0C;&#x4F7F;&#x7528;&#x9017;&#x53F7;&#x5206;&#x5272;&#x5B83;&#x4EEC;&#xFF0C;&#x4F8B;&#x5982;&#xFF1A;case val1, val2, val3&#x3002;</p>
<p>&#x6D41;&#x7A0B;&#x56FE;&#xFF1A;</p>
<center>
    <img style="border-radius: 0.3125em;
    box-shadow: 0 2px 4px 0 rgba(34,36,38,.12),0 2px 10px 0 rgba(34,36,38,.08);" src="../source/images/part9/03.png">
    <br>
    <div style="color:orange; border-bottom: 0px solid #d9d9d9;
    display: inline-block;
    color: #999;ds
    padding: 5px;">switch &#x8BED;&#x53E5;</div>
</center>


<p>&#x5B9E;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">package</span> main

<span class="hljs-keyword">import</span> <span class="hljs-string">&quot;fmt&quot;</span>

<span class="hljs-keyword">func</span> main() {
    <span class="hljs-comment">/* &#x5B9A;&#x4E49;&#x5C40;&#x90E8;&#x53D8;&#x91CF; */</span>
    <span class="hljs-keyword">var</span> grade <span class="hljs-keyword">string</span> = <span class="hljs-string">&quot;B&quot;</span>
    <span class="hljs-keyword">var</span> marks <span class="hljs-keyword">int</span> = <span class="hljs-number">90</span>

    <span class="hljs-keyword">switch</span> marks {
        <span class="hljs-keyword">case</span> <span class="hljs-number">90</span>: grade = <span class="hljs-string">&quot;A&quot;</span>
        <span class="hljs-keyword">case</span> <span class="hljs-number">80</span>: grade = <span class="hljs-string">&quot;B&quot;</span>
        <span class="hljs-keyword">case</span> <span class="hljs-number">50</span>,<span class="hljs-number">60</span>,<span class="hljs-number">70</span> : grade = <span class="hljs-string">&quot;C&quot;</span>
        <span class="hljs-keyword">default</span>: grade = <span class="hljs-string">&quot;D&quot;</span>  
    }

    <span class="hljs-keyword">switch</span> {
        <span class="hljs-keyword">case</span> grade == <span class="hljs-string">&quot;A&quot;</span> :
            fmt.Printf(<span class="hljs-string">&quot;&#x4F18;&#x79C0;!\n&quot;</span>)    
        <span class="hljs-keyword">case</span> grade == <span class="hljs-string">&quot;B&quot;</span>, grade == <span class="hljs-string">&quot;C&quot;</span> :
            fmt.Printf(<span class="hljs-string">&quot;&#x826F;&#x597D;\n&quot;</span>)      
        <span class="hljs-keyword">case</span> grade == <span class="hljs-string">&quot;D&quot;</span> :
            fmt.Printf(<span class="hljs-string">&quot;&#x53CA;&#x683C;\n&quot;</span>)      
        <span class="hljs-keyword">case</span> grade == <span class="hljs-string">&quot;F&quot;</span>:
            fmt.Printf(<span class="hljs-string">&quot;&#x4E0D;&#x53CA;&#x683C;\n&quot;</span>)
        <span class="hljs-keyword">default</span>:
            fmt.Printf(<span class="hljs-string">&quot;&#x5DEE;\n&quot;</span> );
    }
    fmt.Printf(<span class="hljs-string">&quot;&#x4F60;&#x7684;&#x7B49;&#x7EA7;&#x662F; %s\n&quot;</span>, grade );      
}
</code></pre>
<p>&#x4EE5;&#x4E0A;&#x4EE3;&#x7801;&#x6267;&#x884C;&#x7ED3;&#x679C;&#x4E3A;&#xFF1A;</p>
<pre><code class="lang-sh">&#x4F18;&#x79C0;!
&#x4F60;&#x7684;&#x7B49;&#x7EA7;&#x662F; A
</code></pre>
<h3 id="type-switch">Type Switch</h3>
<p>&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;switch &#x8BED;&#x53E5;&#x8FD8;&#x53EF;&#x4EE5;&#x88AB;&#x7528;&#x4E8E; type-switch &#x6765;&#x5224;&#x65AD;&#x67D0;&#x4E2A; interface &#x53D8;&#x91CF;&#x4E2D;&#x5B9E;&#x9645;&#x5B58;&#x50A8;&#x7684;&#x53D8;&#x91CF;&#x7C7B;&#x578B;&#x3002;</p>
<p>Type Switch &#x8BED;&#x6CD5;&#x683C;&#x5F0F;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">switch</span> x.(<span class="hljs-keyword">type</span>){
    <span class="hljs-keyword">case</span> <span class="hljs-keyword">type</span>:
       statement(s);      
    <span class="hljs-keyword">case</span> <span class="hljs-keyword">type</span>:
       statement(s); 
    <span class="hljs-comment">/* &#x4F60;&#x53EF;&#x4EE5;&#x5B9A;&#x4E49;&#x4EFB;&#x610F;&#x4E2A;&#x6570;&#x7684;case */</span>
    <span class="hljs-keyword">default</span>: <span class="hljs-comment">/* &#x53EF;&#x9009; */</span>
       statement(s);
}
</code></pre>
<p>&#x5B9E;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">package</span> main

<span class="hljs-keyword">import</span> <span class="hljs-string">&quot;fmt&quot;</span>

<span class="hljs-keyword">func</span> main() {
    <span class="hljs-keyword">var</span> x <span class="hljs-keyword">interface</span>{}

    <span class="hljs-comment">// x = true</span>

    <span class="hljs-keyword">switch</span> i := x.(<span class="hljs-keyword">type</span>) {
        <span class="hljs-keyword">case</span> <span class="hljs-literal">nil</span>:
            fmt.Printf(<span class="hljs-string">&quot; x &#x7684;&#x7C7B;&#x578B; :%T&quot;</span>,i)
        <span class="hljs-keyword">case</span> <span class="hljs-keyword">int</span>:
            fmt.Printf(<span class="hljs-string">&quot;x &#x662F; int &#x578B;&quot;</span>)
        <span class="hljs-keyword">case</span> <span class="hljs-keyword">float64</span>:
            fmt.Printf(<span class="hljs-string">&quot;x &#x662F; float64 &#x578B;&quot;</span>)
        <span class="hljs-keyword">case</span> <span class="hljs-keyword">func</span>(<span class="hljs-keyword">int</span>) <span class="hljs-keyword">float64</span>:
            fmt.Printf(<span class="hljs-string">&quot;x &#x662F; func(int) &#x578B;&quot;</span>)
        <span class="hljs-keyword">case</span> <span class="hljs-keyword">bool</span>, <span class="hljs-keyword">string</span>:
            fmt.Printf(<span class="hljs-string">&quot;x &#x662F; bool &#x6216; string &#x578B;&quot;</span> )
        <span class="hljs-keyword">default</span>:
            fmt.Printf(<span class="hljs-string">&quot;&#x672A;&#x77E5;&#x578B;&quot;</span>)
    }
}
</code></pre>
<p>&#x4EE5;&#x4E0A;&#x4EE3;&#x7801;&#x6267;&#x884C;&#x7ED3;&#x679C;&#x4E3A;&#xFF1A;</p>
<pre><code class="lang-sh">x &#x7684;&#x7C7B;&#x578B; :&lt;nil&gt;
</code></pre>
<h3 id="fallthrough">fallthrough</h3>
<p>&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#x4F7F;&#x7528; fallthrough &#x4F1A;&#x5F3A;&#x5236;&#x6267;&#x884C;&#x540E;&#x9762;&#x7684; case &#x8BED;&#x53E5;&#xFF0C;fallthrough &#x4E0D;&#x4F1A;&#x5224;&#x65AD;&#x4E0B;&#x4E00;&#x6761; case &#x7684;&#x8868;&#x8FBE;&#x5F0F;&#x7ED3;&#x679C;&#x662F;&#x5426;&#x4E3A; true&#x3002;</p>
<p>&#x5B9E;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">package</span> main

<span class="hljs-keyword">import</span> <span class="hljs-string">&quot;fmt&quot;</span>

<span class="hljs-keyword">func</span> main() {

    <span class="hljs-keyword">switch</span> {
        <span class="hljs-keyword">case</span> <span class="hljs-literal">false</span>:
            fmt.Println(<span class="hljs-string">&quot;1&#x3001;case &#x6761;&#x4EF6;&#x8BED;&#x53E5;&#x4E3A; false&quot;</span>)
            <span class="hljs-keyword">fallthrough</span>
        <span class="hljs-keyword">case</span> <span class="hljs-literal">true</span>:
            fmt.Println(<span class="hljs-string">&quot;2&#x3001;case &#x6761;&#x4EF6;&#x8BED;&#x53E5;&#x4E3A; true&quot;</span>)
            <span class="hljs-keyword">fallthrough</span>
        <span class="hljs-keyword">case</span> <span class="hljs-literal">false</span>:
            fmt.Println(<span class="hljs-string">&quot;3&#x3001;case &#x6761;&#x4EF6;&#x8BED;&#x53E5;&#x4E3A; false&quot;</span>)
            <span class="hljs-keyword">fallthrough</span>
        <span class="hljs-keyword">case</span> <span class="hljs-literal">true</span>:
            fmt.Println(<span class="hljs-string">&quot;4&#x3001;case &#x6761;&#x4EF6;&#x8BED;&#x53E5;&#x4E3A; true&quot;</span>)
        <span class="hljs-keyword">case</span> <span class="hljs-literal">false</span>:
            fmt.Println(<span class="hljs-string">&quot;5&#x3001;case &#x6761;&#x4EF6;&#x8BED;&#x53E5;&#x4E3A; false&quot;</span>)
            <span class="hljs-keyword">fallthrough</span>
        <span class="hljs-keyword">default</span>:
            fmt.Println(<span class="hljs-string">&quot;6&#x3001;&#x9ED8;&#x8BA4; case&quot;</span>)
    }
}
</code></pre>
<p>&#x4EE5;&#x4E0A;&#x4EE3;&#x7801;&#x6267;&#x884C;&#x7ED3;&#x679C;&#x4E3A;&#xFF1A;</p>
<pre><code class="lang-sh">2&#x3001;<span class="hljs-keyword">case</span> &#x6761;&#x4EF6;&#x8BED;&#x53E5;&#x4E3A; <span class="hljs-literal">true</span>
3&#x3001;<span class="hljs-keyword">case</span> &#x6761;&#x4EF6;&#x8BED;&#x53E5;&#x4E3A; <span class="hljs-literal">false</span>
4&#x3001;<span class="hljs-keyword">case</span> &#x6761;&#x4EF6;&#x8BED;&#x53E5;&#x4E3A; <span class="hljs-literal">true</span>
</code></pre>
<p>&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#x4ECE;&#x4EE5;&#x4E0A;&#x4EE3;&#x7801;&#x8F93;&#x51FA;&#x7684;&#x7ED3;&#x679C;&#x53EF;&#x4EE5;&#x770B;&#x51FA;&#xFF1A;switch &#x4ECE;&#x7B2C;&#x4E00;&#x4E2A;&#x5224;&#x65AD;&#x8868;&#x8FBE;&#x5F0F;&#x4E3A; true &#x7684; case &#x5F00;&#x59CB;&#x6267;&#x884C;&#xFF0C;&#x5982;&#x679C; case &#x5E26;&#x6709; fallthrough&#xFF0C;&#x7A0B;&#x5E8F;&#x4F1A;&#x7EE7;&#x7EED;&#x6267;&#x884C;&#x4E0B;&#x4E00;&#x6761; case&#xFF0C;&#x4E14;&#x5B83;&#x4E0D;&#x4F1A;&#x53BB;&#x5224;&#x65AD;&#x4E0B;&#x4E00;&#x4E2A; case &#x7684;&#x8868;&#x8FBE;&#x5F0F;&#x662F;&#x5426;&#x4E3A; true&#x3002;</p>
<h3 id="some-test">some test</h3>
<ol>
<li><p>&#x652F;&#x6301;&#x591A;&#x6761;&#x4EF6;&#x5339;&#x914D;</p>
<pre><code class="lang-go"> <span class="hljs-keyword">switch</span>{
     <span class="hljs-keyword">case</span> <span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>,<span class="hljs-number">4</span>:
     <span class="hljs-keyword">default</span>:
 }
</code></pre>
</li>
<li><p>&#x4E0D;&#x540C;&#x7684; case &#x4E4B;&#x95F4;&#x4E0D;&#x4F7F;&#x7528; break &#x5206;&#x9694;&#xFF0C;&#x9ED8;&#x8BA4;&#x53EA;&#x4F1A;&#x6267;&#x884C;&#x4E00;&#x4E2A; case&#x3002;</p>
</li>
<li><p>&#x5982;&#x679C;&#x60F3;&#x8981;&#x6267;&#x884C;&#x591A;&#x4E2A; case&#xFF0C;&#x9700;&#x8981;&#x4F7F;&#x7528; fallthrough &#x5173;&#x952E;&#x5B57;&#xFF0C;&#x4E5F;&#x53EF;&#x7528; break &#x7EC8;&#x6B62;&#x3002;</p>
<pre><code class="lang-go"> <span class="hljs-keyword">switch</span>{
     <span class="hljs-keyword">case</span> <span class="hljs-number">1</span>:
     ...
     <span class="hljs-keyword">if</span>(...){
         <span class="hljs-keyword">break</span>
     }

     <span class="hljs-keyword">fallthrough</span> <span class="hljs-comment">// &#x6B64;&#x65F6;switch(1)&#x4F1A;&#x6267;&#x884C;case1&#x548C;case2&#xFF0C;&#x4F46;&#x662F;&#x5982;&#x679C;&#x6EE1;&#x8DB3;if&#x6761;&#x4EF6;&#xFF0C;&#x5219;&#x53EA;&#x6267;&#x884C;case1</span>

     <span class="hljs-keyword">case</span> <span class="hljs-number">2</span>:
     ...
     <span class="hljs-keyword">case</span> <span class="hljs-number">3</span>:
 }
</code></pre>
</li>
</ol>
<hr>
<h2 id="select-&#x8BED;&#x53E5;">select &#x8BED;&#x53E5;</h2>
<p>&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;select &#x662F; Go &#x4E2D;&#x7684;&#x4E00;&#x4E2A;&#x63A7;&#x5236;&#x7ED3;&#x6784;&#xFF0C;&#x7C7B;&#x4F3C;&#x4E8E;&#x7528;&#x4E8E;&#x901A;&#x4FE1;&#x7684; switch &#x8BED;&#x53E5;&#x3002;&#x6BCF;&#x4E2A; case &#x5FC5;&#x987B;&#x662F;&#x4E00;&#x4E2A;&#x901A;&#x4FE1;&#x64CD;&#x4F5C;&#xFF0C;&#x8981;&#x4E48;&#x662F;&#x53D1;&#x9001;&#x8981;&#x4E48;&#x662F;&#x63A5;&#x6536;&#x3002;</p>
<p>&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;select &#x968F;&#x673A;&#x6267;&#x884C;&#x4E00;&#x4E2A;&#x53EF;&#x8FD0;&#x884C;&#x7684; case&#x3002;&#x5982;&#x679C;&#x6CA1;&#x6709; case &#x53EF;&#x8FD0;&#x884C;&#xFF0C;&#x5B83;&#x5C06;&#x963B;&#x585E;&#xFF0C;&#x76F4;&#x5230;&#x6709; case &#x53EF;&#x8FD0;&#x884C;&#x3002;&#x4E00;&#x4E2A;&#x9ED8;&#x8BA4;&#x7684;&#x5B50;&#x53E5;&#x5E94;&#x8BE5;&#x603B;&#x662F;&#x53EF;&#x8FD0;&#x884C;&#x7684;&#x3002;</p>
<h3 id="&#x8BED;&#x6CD5;">&#x8BED;&#x6CD5;</h3>
<p>Go &#x7F16;&#x7A0B;&#x8BED;&#x8A00;&#x4E2D; select &#x8BED;&#x53E5;&#x7684;&#x8BED;&#x6CD5;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">select</span> {
    <span class="hljs-keyword">case</span> communication clause  :
       statement(s);      
    <span class="hljs-keyword">case</span> communication clause  :
       statement(s);
    <span class="hljs-comment">/* &#x4F60;&#x53EF;&#x4EE5;&#x5B9A;&#x4E49;&#x4EFB;&#x610F;&#x6570;&#x91CF;&#x7684; case */</span>
    <span class="hljs-keyword">default</span> : <span class="hljs-comment">/* &#x53EF;&#x9009; */</span>
       statement(s);
}
</code></pre>
<p>&#x4EE5;&#x4E0B;&#x63CF;&#x8FF0;&#x4E86; select &#x8BED;&#x53E5;&#x7684;&#x8BED;&#x6CD5;&#xFF1A;</p>
<ul>
<li>&#x6BCF;&#x4E2A; case &#x90FD;&#x5FC5;&#x987B;&#x662F;&#x4E00;&#x4E2A;&#x901A;&#x4FE1;</li>
<li>&#x6240;&#x6709; channel &#x8868;&#x8FBE;&#x5F0F;&#x90FD;&#x4F1A;&#x88AB;&#x6C42;&#x503C;</li>
<li>&#x6240;&#x6709;&#x88AB;&#x53D1;&#x9001;&#x7684;&#x8868;&#x8FBE;&#x5F0F;&#x90FD;&#x4F1A;&#x88AB;&#x6C42;&#x503C;</li>
<li>&#x5982;&#x679C;&#x4EFB;&#x610F;&#x67D0;&#x4E2A;&#x901A;&#x4FE1;&#x53EF;&#x4EE5;&#x8FDB;&#x884C;&#xFF0C;&#x5B83;&#x5C31;&#x6267;&#x884C;&#xFF0C;&#x5176;&#x4ED6;&#x88AB;&#x5FFD;&#x7565;&#x3002;</li>
<li>&#x5982;&#x679C;&#x6709;&#x591A;&#x4E2A; case &#x90FD;&#x53EF;&#x4EE5;&#x8FD0;&#x884C;&#xFF0C;Select &#x4F1A;&#x968F;&#x673A;&#x516C;&#x5E73;&#x5730;&#x9009;&#x51FA;&#x4E00;&#x4E2A;&#x6267;&#x884C;&#x3002;&#x5176;&#x4ED6;&#x4E0D;&#x4F1A;&#x6267;&#x884C;&#x3002;&#x5426;&#x5219;&#xFF1A;<ul>
<li>&#x5982;&#x679C;&#x6709; default &#x5B50;&#x53E5;&#xFF0C;&#x5219;&#x6267;&#x884C;&#x8BE5;&#x8BED;&#x53E5;&#x3002;</li>
<li>&#x5982;&#x679C;&#x6CA1;&#x6709; default &#x5B50;&#x53E5;&#xFF0C;select &#x5C06;&#x963B;&#x585E;&#xFF0C;&#x76F4;&#x5230;&#x67D0;&#x4E2A;&#x901A;&#x4FE1;&#x53EF;&#x4EE5;&#x8FD0;&#x884C;&#xFF1B;Go &#x4E0D;&#x4F1A;&#x91CD;&#x65B0;&#x5BF9; channel &#x6216;&#x503C;&#x8FDB;&#x884C;&#x6C42;&#x503C;</li>
</ul>
</li>
</ul>
<p>&#x5B9E;&#x4F8B;&#xFF1A;select &#x8BED;&#x53E5;&#x5E94;&#x7528;&#x6F14;&#x793A;&#xFF1A;</p>
<pre><code class="lang-go"><span class="hljs-keyword">package</span> main

<span class="hljs-keyword">import</span> <span class="hljs-string">&quot;fmt&quot;</span>

<span class="hljs-keyword">func</span> main() {
    <span class="hljs-keyword">var</span> c1, c2, c3 <span class="hljs-keyword">chan</span> <span class="hljs-keyword">int</span>
    <span class="hljs-keyword">var</span> i1, i2 <span class="hljs-keyword">int</span>
    <span class="hljs-keyword">select</span> {
        <span class="hljs-keyword">case</span> i1 = &lt;-c1:
            fmt.Printf(<span class="hljs-string">&quot;received &quot;</span>, i1, <span class="hljs-string">&quot; from c1\n&quot;</span>)
        <span class="hljs-keyword">case</span> c2 &lt;- i2:
            fmt.Printf(<span class="hljs-string">&quot;sent &quot;</span>, i2, <span class="hljs-string">&quot; to c2\n&quot;</span>)
        <span class="hljs-keyword">case</span> i3, ok := (&lt;-c3):  <span class="hljs-comment">// same as: i3, ok := &lt;-c3</span>
            <span class="hljs-keyword">if</span> ok {
                fmt.Printf(<span class="hljs-string">&quot;received &quot;</span>, i3, <span class="hljs-string">&quot; from c3\n&quot;</span>)
            } <span class="hljs-keyword">else</span> {
                fmt.Printf(<span class="hljs-string">&quot;c3 is closed\n&quot;</span>)
            }
        <span class="hljs-keyword">default</span>:
            fmt.Printf(<span class="hljs-string">&quot;no communication\n&quot;</span>)
    }    
}
</code></pre>
<p>&#x4EE5;&#x4E0A;&#x4EE3;&#x7801;&#x6267;&#x884C;&#x7ED3;&#x679C;&#x4E3A;&#xFF1A;</p>
<pre><code class="lang-sh">no communication
</code></pre>
<p><code>The concept of it is to create order out of chaos based on random drawing of titles.</code></p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="../part8/" class="navigation navigation-prev " aria-label="Previous page: Go 语言运算符">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="../part10/" class="navigation navigation-next " aria-label="Next page: Go 语言循环语句">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"Go 语言条件语句","level":"1.10","depth":1,"next":{"title":"Go 语言循环语句","level":"1.11","depth":1,"path":"part10/README.md","ref":"part10/README.md","articles":[]},"previous":{"title":"Go 语言运算符","level":"1.9","depth":1,"path":"part8/README.md","ref":"part8/README.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"part9/README.md","mtime":"2021-01-20T01:47:57.720Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2021-01-21T06:29:04.898Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

